package com.swak.frame.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class ImagePathUtil {

    private static String imageDomain = "https://img30.xxx.com/img/";

    private static String imageDomainMoble = "https://m.xxx.com/img/";


    // 打算图片用的模 e.g n1,n2,n3,n4 ，n5,n6,n7,n8 8个值就用8
    private static final int MAX_FIRST_GROUP_STATIC_DOMAIN = 8;

    // 打算图片用的模 e.g img10,img11,img12,img13,img14 5个值
    private static final int MAX_FIRST_GROUP1_STATIC_DOMAIN = 5;

    // 匹配图片的正则
    private static final Pattern DX_GROUP_STATIC_DOMAIN_PATTERN =
        Pattern.compile("https://img(\\d+).xxx.com");

    private static final Pattern DX_GROUP_STATIC_DOMAIN_PATTERN_N =
        Pattern.compile("xxx.com/n(\\d+)");



    /**
     * 
     * @Title: formatImgDomain
     * @Description: 散列取图片
     * @param @param oriUrl
     * @param @return 设定文件
     * @return String 返回类型
     * @author wanghui3
     * @date 2013-5-29
     * @throws
     */
    public static String formatImgDomain(String oriUrl) {
        if (StringUtils.isBlank(oriUrl)) {
            return "";
        }
        // 哈希计算分配第一组静态服务器，由dx(x所有数字)哈希为d6,d7,d8,d9
        Matcher firstMatcher = DX_GROUP_STATIC_DOMAIN_PATTERN.matcher(oriUrl);
        if (firstMatcher.find()) {
            int n = Math.abs(oriUrl.hashCode() % MAX_FIRST_GROUP1_STATIC_DOMAIN) + 10;
            oriUrl =
                oriUrl.replaceFirst(firstMatcher.group(0), "https://img" + n + ".xxx.com");
        }
        return oriUrl;
    }

    public static String formatImgBusi(String oriUrl) {
        if (StringUtils.isBlank(oriUrl)) {
            return "";
        }
        // 哈希计算分配第一组静态服务器，由dx(x所有数字)哈希为d6,d7,d8,d9
        Matcher firstMatcher = DX_GROUP_STATIC_DOMAIN_PATTERN_N.matcher(oriUrl);
        if (firstMatcher.find()) {
            int n = Math.abs(oriUrl.hashCode() % MAX_FIRST_GROUP_STATIC_DOMAIN) + 1;
            oriUrl = oriUrl.replaceFirst(firstMatcher.group(0), "xxx.com/n" + n);
        }
        return oriUrl;
    }


    public static String formatImgDomainBusi(String oriUrl) {
        if (StringUtils.isBlank(oriUrl)) {
            return "";
        }
        oriUrl = formatImgDomain(oriUrl);
        oriUrl = formatImgBusi(oriUrl);

        return oriUrl;
    }

    public static String getImgPath(String imgSource) {
        if (StringUtils.isEmpty(imgSource)) {
            return imgSource;
        }

        if (imgSource.indexOf("xxx.com") > -1) {
            return formatImgDomain(imgSource);
        }
        return formatImgDomain(imageDomain + imgSource);
    }


    public static String getMobleImgPath(String imgSource) {
        if (StringUtils.isEmpty(imgSource)) {
            return imgSource;
        }

        if (imgSource.indexOf("xxx.com") > -1) {
            return formatImgDomain(imgSource);
        }
        return formatImgDomain(imageDomainMoble + imgSource);
    }

    public static void main(String[] args) {
        String img = "jfs/t1/113905/32/7661/374179/5ec64f2cE965696ce/38943e51484390fd.jpg";
        System.err.println(getMobleImgPath(img));
    }
}
